﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace QuanLyPhongKham
{
	public class frmKhaitondau : BaseUserControl
	{
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.ComboBox manguon;
        private LibList.List listDMBD;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label lTen;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.Label label18;
		private System.Windows.Forms.TextBox tenbd;
        private System.Windows.Forms.TextBox mabd;
		private string s_mmyy,sql,user;
		private int i_makho,i_mabd,i_userid;
		private long l_id;
		private decimal d_soluong,d_sotien,d_slnhap,d_slxuat,d_giaban,d_soluongcu,d_dongia;
		private DAL.AccessData d;
		private DataTable dtdmbd=new DataTable();
        private DataTable dtsave = new DataTable();
		private DataTable dt=new DataTable();
		private DataTable dtdmnx=new DataTable();
		private System.Windows.Forms.Label ldvt;
		private DataRow r;
		private System.Windows.Forms.TextBox stt;
		private System.Windows.Forms.Label label15;
        private System.Windows.Forms.Label label24;
		private System.Windows.Forms.TextBox tenhc;
        private System.Windows.Forms.Label lTenhc;
        private System.Windows.Forms.Label label25;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox timkiem;
        private DataGrid dataGrid1;
        private Panel bNut;
        private Button butKetthuc;
        private Button butChon;
        private Button butHuy;
        private Button butSua;
        private Button butMoi;
        private Button butLuu;
        private Button butIn;
        private Button butBoqua;
        private TextBox handung;
        private TextBox soluong;
        private TextBox losx;
        private TextBox dongia;
        private TextBox sotien;
        private TextBox dvt;
        private TextBox nhacc;
        private TextBox giaban;
        private LibList.List listnhacc;
        private TextBox mancc;
		private System.ComponentModel.Container components = null;

		public frmKhaitondau(DAL.AccessData acc,string mmyy,int kho,int userid)
		{
			InitializeComponent();
            d = acc; i_makho = kho; s_mmyy = mmyy; i_userid = userid;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.label9 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.manguon = new System.Windows.Forms.ComboBox();
            this.listDMBD = new LibList.List();
            this.label13 = new System.Windows.Forms.Label();
            this.lTen = new System.Windows.Forms.Label();
            this.ldvt = new System.Windows.Forms.Label();
            this.label16 = new System.Windows.Forms.Label();
            this.label18 = new System.Windows.Forms.Label();
            this.tenbd = new System.Windows.Forms.TextBox();
            this.mabd = new System.Windows.Forms.TextBox();
            this.stt = new System.Windows.Forms.TextBox();
            this.label15 = new System.Windows.Forms.Label();
            this.label24 = new System.Windows.Forms.Label();
            this.tenhc = new System.Windows.Forms.TextBox();
            this.lTenhc = new System.Windows.Forms.Label();
            this.label25 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.timkiem = new System.Windows.Forms.TextBox();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.bNut = new System.Windows.Forms.Panel();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butChon = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butMoi = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butIn = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.handung = new System.Windows.Forms.TextBox();
            this.soluong = new System.Windows.Forms.TextBox();
            this.losx = new System.Windows.Forms.TextBox();
            this.dongia = new System.Windows.Forms.TextBox();
            this.sotien = new System.Windows.Forms.TextBox();
            this.dvt = new System.Windows.Forms.TextBox();
            this.nhacc = new System.Windows.Forms.TextBox();
            this.giaban = new System.Windows.Forms.TextBox();
            this.listnhacc = new LibList.List();
            this.mancc = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.bNut.SuspendLayout();
            this.SuspendLayout();
            // 
            // label9
            // 
            this.label9.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(266, 371);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(45, 13);
            this.label9.TabIndex = 12;
            this.label9.Text = "Nguồn :";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label10
            // 
            this.label10.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(414, 371);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(81, 13);
            this.label10.TabIndex = 14;
            this.label10.Text = "Nhà cung cấp :";
            this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // manguon
            // 
            this.manguon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.manguon.BackColor = System.Drawing.SystemColors.HighlightText;
            this.manguon.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.manguon.Enabled = false;
            this.manguon.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.manguon.Location = new System.Drawing.Point(310, 366);
            this.manguon.Name = "manguon";
            this.manguon.Size = new System.Drawing.Size(104, 21);
            this.manguon.TabIndex = 13;
            this.manguon.SelectedIndexChanged += new System.EventHandler(this.manguon_SelectedIndexChanged);
            this.manguon.KeyDown += new System.Windows.Forms.KeyEventHandler(this.manguon_KeyDown);
            // 
            // listDMBD
            // 
            this.listDMBD.BackColor = System.Drawing.SystemColors.Info;
            this.listDMBD.ColumnCount = 0;
            this.listDMBD.Location = new System.Drawing.Point(122, 415);
            this.listDMBD.MatchBufferTimeOut = 1000;
            this.listDMBD.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.listDMBD.Name = "listDMBD";
            this.listDMBD.Size = new System.Drawing.Size(75, 17);
            this.listDMBD.TabIndex = 26;
            this.listDMBD.TextIndex = -1;
            this.listDMBD.TextMember = null;
            this.listDMBD.ValueIndex = -1;
            this.listDMBD.Visible = false;
            this.listDMBD.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listDMBD_KeyDown);
            // 
            // label13
            // 
            this.label13.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label13.AutoSize = true;
            this.label13.Location = new System.Drawing.Point(1, 348);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(58, 13);
            this.label13.TabIndex = 0;
            this.label13.Text = "Mã thuốc :";
            this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lTen
            // 
            this.lTen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lTen.AutoSize = true;
            this.lTen.Location = new System.Drawing.Point(119, 348);
            this.lTen.Name = "lTen";
            this.lTen.Size = new System.Drawing.Size(62, 13);
            this.lTen.TabIndex = 2;
            this.lTen.Text = "Tên thuốc :";
            this.lTen.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ldvt
            // 
            this.ldvt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.ldvt.AutoSize = true;
            this.ldvt.Location = new System.Drawing.Point(539, 348);
            this.ldvt.Name = "ldvt";
            this.ldvt.Size = new System.Drawing.Size(35, 13);
            this.ldvt.TabIndex = 6;
            this.ldvt.Text = "ĐVT :";
            this.ldvt.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label16
            // 
            this.label16.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label16.AutoSize = true;
            this.label16.Location = new System.Drawing.Point(4, 394);
            this.label16.Name = "label16";
            this.label16.Size = new System.Drawing.Size(55, 13);
            this.label16.TabIndex = 17;
            this.label16.Text = "Số lượng :";
            this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label18
            // 
            this.label18.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label18.AutoSize = true;
            this.label18.Location = new System.Drawing.Point(265, 394);
            this.label18.Name = "label18";
            this.label18.Size = new System.Drawing.Size(46, 13);
            this.label18.TabIndex = 21;
            this.label18.Text = "Số tiền :";
            this.label18.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tenbd
            // 
            this.tenbd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tenbd.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tenbd.Enabled = false;
            this.tenbd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tenbd.Location = new System.Drawing.Point(180, 343);
            this.tenbd.Name = "tenbd";
            this.tenbd.Size = new System.Drawing.Size(165, 21);
            this.tenbd.TabIndex = 3;
            this.tenbd.TextChanged += new System.EventHandler(this.tenbd_TextChanged);
            this.tenbd.Validated += new System.EventHandler(this.tenbd_Validated);
            this.tenbd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tenbd_KeyDown);
            // 
            // mabd
            // 
            this.mabd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.mabd.BackColor = System.Drawing.SystemColors.HighlightText;
            this.mabd.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.mabd.Enabled = false;
            this.mabd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.mabd.Location = new System.Drawing.Point(57, 343);
            this.mabd.Name = "mabd";
            this.mabd.Size = new System.Drawing.Size(63, 21);
            this.mabd.TabIndex = 1;
            this.mabd.TextChanged += new System.EventHandler(this.mabd_TextChanged);
            this.mabd.Validated += new System.EventHandler(this.mabd_Validated);
            this.mabd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tenbd_KeyDown);
            // 
            // stt
            // 
            this.stt.Enabled = false;
            this.stt.Location = new System.Drawing.Point(106, 267);
            this.stt.Name = "stt";
            this.stt.Size = new System.Drawing.Size(40, 20);
            this.stt.TabIndex = 60;
            // 
            // label15
            // 
            this.label15.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label15.AutoSize = true;
            this.label15.Location = new System.Drawing.Point(-1, 371);
            this.label15.Name = "label15";
            this.label15.Size = new System.Drawing.Size(60, 13);
            this.label15.TabIndex = 8;
            this.label15.Text = "Hạn dùng :";
            this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label24
            // 
            this.label24.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label24.AutoSize = true;
            this.label24.Location = new System.Drawing.Point(143, 371);
            this.label24.Name = "label24";
            this.label24.Size = new System.Drawing.Size(38, 13);
            this.label24.TabIndex = 10;
            this.label24.Text = "Lô sx :";
            this.label24.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tenhc
            // 
            this.tenhc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.tenhc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tenhc.Enabled = false;
            this.tenhc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tenhc.Location = new System.Drawing.Point(406, 343);
            this.tenhc.Name = "tenhc";
            this.tenhc.Size = new System.Drawing.Size(132, 21);
            this.tenhc.TabIndex = 5;
            // 
            // lTenhc
            // 
            this.lTenhc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.lTenhc.AutoSize = true;
            this.lTenhc.Location = new System.Drawing.Point(348, 348);
            this.lTenhc.Name = "lTenhc";
            this.lTenhc.Size = new System.Drawing.Size(60, 13);
            this.lTenhc.TabIndex = 4;
            this.lTenhc.Text = "Hoạt chất :";
            this.lTenhc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label25
            // 
            this.label25.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label25.AutoSize = true;
            this.label25.Location = new System.Drawing.Point(444, 394);
            this.label25.Name = "label25";
            this.label25.Size = new System.Drawing.Size(50, 13);
            this.label25.TabIndex = 23;
            this.label25.Text = "Giá bán :";
            this.label25.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(131, 394);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(50, 13);
            this.label1.TabIndex = 19;
            this.label1.Text = "Đơn giá :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // timkiem
            // 
            this.timkiem.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.timkiem.ForeColor = System.Drawing.Color.Red;
            this.timkiem.Location = new System.Drawing.Point(1, 1);
            this.timkiem.Name = "timkiem";
            this.timkiem.Size = new System.Drawing.Size(626, 20);
            this.timkiem.TabIndex = 68;
            this.timkiem.Text = "Tìm kiếm";
            this.timkiem.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.timkiem.TextChanged += new System.EventHandler(this.tim_TextChanged);
            this.timkiem.Validated += new System.EventHandler(this.timkiem_Validated);
            this.timkiem.MouseDown += new System.Windows.Forms.MouseEventHandler(this.timkiem_MouseDown);
            this.timkiem.Enter += new System.EventHandler(this.tim_Enter);
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.Color.White;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.Color.White;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.CaptionVisible = false;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(2, 23);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 5;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(624, 314);
            this.dataGrid1.TabIndex = 71;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // bNut
            // 
            this.bNut.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.bNut.Controls.Add(this.butKetthuc);
            this.bNut.Controls.Add(this.butChon);
            this.bNut.Controls.Add(this.butHuy);
            this.bNut.Controls.Add(this.butSua);
            this.bNut.Controls.Add(this.butMoi);
            this.bNut.Controls.Add(this.butLuu);
            this.bNut.Controls.Add(this.butIn);
            this.bNut.Controls.Add(this.butBoqua);
            this.bNut.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.bNut.Location = new System.Drawing.Point(1, 435);
            this.bNut.Name = "bNut";
            this.bNut.Size = new System.Drawing.Size(625, 34);
            this.bNut.TabIndex = 24;
            this.bNut.SizeChanged += new System.EventHandler(this.bNut_SizeChanged);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetthuc.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
            this.butKetthuc.Image = global::QuanLyPhongKham.Properties.Resources.butKetthuc1;
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(465, 2);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(78, 31);
            this.butKetthuc.TabIndex = 8;
            this.butKetthuc.Text = "      &Kết thúc";
            this.butKetthuc.UseVisualStyleBackColor = true;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butChon
            // 
            this.butChon.Image = global::QuanLyPhongKham.Properties.Resources.Ok;
            this.butChon.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butChon.Location = new System.Drawing.Point(72, 38);
            this.butChon.Name = "butChon";
            this.butChon.Size = new System.Drawing.Size(83, 31);
            this.butChon.TabIndex = 60;
            this.butChon.Text = "    &Đơn thuốc";
            this.butChon.UseVisualStyleBackColor = true;
            this.butChon.Visible = false;
            // 
            // butHuy
            // 
            this.butHuy.Image = global::QuanLyPhongKham.Properties.Resources.butXoa;
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(338, 2);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(65, 31);
            this.butHuy.TabIndex = 6;
            this.butHuy.Text = "   &Hủy";
            this.butHuy.UseVisualStyleBackColor = true;
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butSua
            // 
            this.butSua.Image = global::QuanLyPhongKham.Properties.Resources.butSua;
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(201, 2);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(62, 31);
            this.butSua.TabIndex = 4;
            this.butSua.Text = "     &Sửa";
            this.butSua.UseVisualStyleBackColor = true;
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butMoi
            // 
            this.butMoi.Image = global::QuanLyPhongKham.Properties.Resources.butMoi;
            this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butMoi.Location = new System.Drawing.Point(77, 2);
            this.butMoi.Name = "butMoi";
            this.butMoi.Size = new System.Drawing.Size(62, 31);
            this.butMoi.TabIndex = 1;
            this.butMoi.Text = "     &Mới";
            this.butMoi.UseVisualStyleBackColor = true;
            this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
            // 
            // butLuu
            // 
            this.butLuu.Image = global::QuanLyPhongKham.Properties.Resources.butLuu1;
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(139, 2);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(62, 31);
            this.butLuu.TabIndex = 0;
            this.butLuu.Text = "     &Lưu";
            this.butLuu.UseVisualStyleBackColor = true;
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butIn
            // 
            this.butIn.Image = global::QuanLyPhongKham.Properties.Resources.butIn;
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(403, 2);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(62, 31);
            this.butIn.TabIndex = 7;
            this.butIn.Text = "      &In";
            this.butIn.UseVisualStyleBackColor = true;
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Image = global::QuanLyPhongKham.Properties.Resources.butBoqua;
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(263, 2);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(75, 31);
            this.butBoqua.TabIndex = 5;
            this.butBoqua.Text = "     &Bỏ qua";
            this.butBoqua.UseVisualStyleBackColor = true;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // handung
            // 
            this.handung.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.handung.BackColor = System.Drawing.SystemColors.HighlightText;
            this.handung.Enabled = false;
            this.handung.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.handung.Location = new System.Drawing.Point(57, 366);
            this.handung.Name = "handung";
            this.handung.Size = new System.Drawing.Size(63, 21);
            this.handung.TabIndex = 9;
            this.handung.KeyDown += new System.Windows.Forms.KeyEventHandler(this.handung_KeyDown);
            this.handung.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.handung_KeyPress);
            // 
            // soluong
            // 
            this.soluong.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.soluong.BackColor = System.Drawing.SystemColors.HighlightText;
            this.soluong.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.soluong.Enabled = false;
            this.soluong.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.soluong.Location = new System.Drawing.Point(57, 389);
            this.soluong.Name = "soluong";
            this.soluong.Size = new System.Drawing.Size(63, 21);
            this.soluong.TabIndex = 18;
            this.soluong.Validated += new System.EventHandler(this.soluong_Validated);
            this.soluong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.handung_KeyDown);
            this.soluong.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dongia_KeyPress);
            // 
            // losx
            // 
            this.losx.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.losx.BackColor = System.Drawing.SystemColors.HighlightText;
            this.losx.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.losx.Enabled = false;
            this.losx.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.losx.Location = new System.Drawing.Point(180, 366);
            this.losx.Name = "losx";
            this.losx.Size = new System.Drawing.Size(84, 21);
            this.losx.TabIndex = 11;
            this.losx.KeyDown += new System.Windows.Forms.KeyEventHandler(this.losx_KeyDown);
            // 
            // dongia
            // 
            this.dongia.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.dongia.BackColor = System.Drawing.SystemColors.HighlightText;
            this.dongia.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.dongia.Enabled = false;
            this.dongia.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dongia.Location = new System.Drawing.Point(180, 389);
            this.dongia.Name = "dongia";
            this.dongia.Size = new System.Drawing.Size(84, 21);
            this.dongia.TabIndex = 20;
            this.dongia.Validated += new System.EventHandler(this.dongia_Validated);
            this.dongia.KeyDown += new System.Windows.Forms.KeyEventHandler(this.handung_KeyDown);
            this.dongia.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dongia_KeyPress);
            // 
            // sotien
            // 
            this.sotien.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.sotien.BackColor = System.Drawing.SystemColors.HighlightText;
            this.sotien.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.sotien.Enabled = false;
            this.sotien.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sotien.Location = new System.Drawing.Point(310, 389);
            this.sotien.Name = "sotien";
            this.sotien.Size = new System.Drawing.Size(104, 21);
            this.sotien.TabIndex = 22;
            this.sotien.Validated += new System.EventHandler(this.sotien_Validated);
            this.sotien.KeyDown += new System.Windows.Forms.KeyEventHandler(this.handung_KeyDown);
            this.sotien.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dongia_KeyPress);
            // 
            // dvt
            // 
            this.dvt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.dvt.BackColor = System.Drawing.SystemColors.HighlightText;
            this.dvt.Enabled = false;
            this.dvt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dvt.Location = new System.Drawing.Point(572, 343);
            this.dvt.Name = "dvt";
            this.dvt.Size = new System.Drawing.Size(55, 21);
            this.dvt.TabIndex = 7;
            // 
            // nhacc
            // 
            this.nhacc.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.nhacc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.nhacc.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.nhacc.Enabled = false;
            this.nhacc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.nhacc.Location = new System.Drawing.Point(527, 366);
            this.nhacc.Name = "nhacc";
            this.nhacc.Size = new System.Drawing.Size(100, 21);
            this.nhacc.TabIndex = 16;
            this.nhacc.TextChanged += new System.EventHandler(this.nhacc_TextChanged);
            this.nhacc.Validated += new System.EventHandler(this.nhacc_Validated);
            this.nhacc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.nhacc_KeyDown);
            // 
            // giaban
            // 
            this.giaban.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.giaban.BackColor = System.Drawing.SystemColors.HighlightText;
            this.giaban.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.giaban.Enabled = false;
            this.giaban.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.giaban.Location = new System.Drawing.Point(493, 389);
            this.giaban.Name = "giaban";
            this.giaban.Size = new System.Drawing.Size(134, 21);
            this.giaban.TabIndex = 24;
            this.giaban.Validated += new System.EventHandler(this.giaban_Validated);
            this.giaban.KeyDown += new System.Windows.Forms.KeyEventHandler(this.handung_KeyDown);
            this.giaban.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dongia_KeyPress);
            // 
            // listnhacc
            // 
            this.listnhacc.BackColor = System.Drawing.SystemColors.Info;
            this.listnhacc.ColumnCount = 0;
            this.listnhacc.Location = new System.Drawing.Point(264, 412);
            this.listnhacc.MatchBufferTimeOut = 1000;
            this.listnhacc.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.listnhacc.Name = "listnhacc";
            this.listnhacc.Size = new System.Drawing.Size(75, 17);
            this.listnhacc.TabIndex = 81;
            this.listnhacc.TextIndex = -1;
            this.listnhacc.TextMember = null;
            this.listnhacc.ValueIndex = -1;
            this.listnhacc.Visible = false;
            // 
            // mancc
            // 
            this.mancc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.mancc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.mancc.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.mancc.Enabled = false;
            this.mancc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.mancc.Location = new System.Drawing.Point(493, 366);
            this.mancc.Name = "mancc";
            this.mancc.Size = new System.Drawing.Size(33, 21);
            this.mancc.TabIndex = 15;
            this.mancc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.handung_KeyDown);
            this.mancc.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.handung_KeyPress);
            // 
            // frmKhaitondau
            // 
            //this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(628, 470);
            this.Controls.Add(this.mancc);
            this.Controls.Add(this.listnhacc);
            this.Controls.Add(this.giaban);
            this.Controls.Add(this.nhacc);
            this.Controls.Add(this.dvt);
            this.Controls.Add(this.sotien);
            this.Controls.Add(this.dongia);
            this.Controls.Add(this.losx);
            this.Controls.Add(this.soluong);
            this.Controls.Add(this.handung);
            this.Controls.Add(this.bNut);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.timkiem);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.mabd);
            this.Controls.Add(this.label25);
            this.Controls.Add(this.tenbd);
            this.Controls.Add(this.tenhc);
            this.Controls.Add(this.lTenhc);
            this.Controls.Add(this.label24);
            this.Controls.Add(this.label15);
            this.Controls.Add(this.label18);
            this.Controls.Add(this.label16);
            this.Controls.Add(this.ldvt);
            this.Controls.Add(this.lTen);
            this.Controls.Add(this.label13);
            this.Controls.Add(this.manguon);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.listDMBD);
            this.Controls.Add(this.stt);
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "frmKhaitondau";
            //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Tồn đầu";
            this.Load += new System.EventHandler(this.frmKhaitondau_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.bNut.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmKhaitondau_Load(object sender, System.EventArgs e)
		{
            user = d.user; 
			manguon.DisplayMember="TEN";
			manguon.ValueMember="ID";
            manguon.DataSource = d.get_data("select * from " + user + ".d_dmnguon order by id").Tables[0]; ;
			listnhacc.DisplayMember="ID";
            listnhacc.ValueMember = "TEN";
            dtdmnx = d.get_data("select * from " + user + ".d_dmnx order by id").Tables[0];
            listnhacc.DataSource = dtdmnx;
			listDMBD.DisplayMember="TEN";
			listDMBD.ValueMember="MA";
			load_grid();
			AddGridTableStyle();
			ref_text(0);
		}

		private void load_grid()
		{
			sql="select 1 as tt,a.stt,a.manguon,c.ten as tennguon,a.madv,d.ten as tennhacc,b.ma,trim(b.ten)||' '||b.hamluong as tenbd,b.tenhc,";
            sql+="b.dang,a.handung,a.losx,a.tondau,round(a.tondau*a.giamua,3) as sttondau,a.slnhap,a.slxuat,a.giaban,a.giamua as dongia ";
            sql+=" from " + user + ".d_tonkhoct_" + s_mmyy + " a," + user + ".d_dmbd b," + user + ".d_dmnguon c," + user + ".d_dmnx d";
			sql+=" where a.mabd=b.id and a.manguon=c.id and a.madv=d.id and a.tondau<>0 and a.makho="+i_makho+" order by a.stt";
			dt=d.get_data(sql).Tables[0];
            //string mmyyt = d.Mmyy_truoc(s_mmyy);
            //if (d.bMmyy(mmyyt))
            //{
            //    DataRow r1;
            //    DataTable tmp = d.get_data("select stt from "+user+mmyyt+".d_tonkhoct where (tondau<>0 or slnhap<>0 or slxuat<>0) and (makho=" + i_makho + ")").Tables[0];
            //    foreach (DataRow r in dt.Rows)
            //    {
            //        r1 = d.getrowbyid(tmp, "stt=" + long.Parse(r["stt"].ToString()));
            //        if (r1 == null) r["tt"] = 0;
            //    }
            //}
			dataGrid1.DataSource=dt;
		}
        private void AddGridTableStyle()
        {
            DataGridColoredTextBoxColumn TextCol;
            delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.RowHeaderWidth = 5;
            ts.AllowSorting = false;

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "stt";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 1);
            TextCol.MappingName = "tennguon";
            TextCol.HeaderText = "Nguồn";
            TextCol.Width = 80;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 2);
            TextCol.MappingName = "tennhacc";
            TextCol.HeaderText = "Nhà cung cấp";
            TextCol.Width = 150;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 3);
            TextCol.MappingName = "ma";
            TextCol.HeaderText = "Mã số";
            TextCol.Width = 50;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 4);
            TextCol.MappingName = "tenbd";
            TextCol.HeaderText = "Tên thuốc - hàm lượng";
            TextCol.Width =  230;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 5);
            TextCol.MappingName = "tenhc";
            TextCol.HeaderText = "Hoạt chất";
            TextCol.Width = 200;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 6);
            TextCol.MappingName = "dang";
            TextCol.HeaderText = "ĐVT";
            TextCol.Width = 50;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 7);
            TextCol.MappingName = "handung";
            TextCol.HeaderText = "Date";
            TextCol.Width = 30;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 8);
            TextCol.MappingName = "losx";
            TextCol.HeaderText = "Lô SX";
            TextCol.Width = 50;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 9);
            TextCol.MappingName = "tondau";
            TextCol.HeaderText = "Tồn đầu";
            TextCol.Width = 80;
            TextCol.Format = "###,###,##0.0";
            TextCol.Alignment = HorizontalAlignment.Right;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 10);
            TextCol.MappingName = "dongia";
            TextCol.HeaderText = "Đơn giá";
            TextCol.Width = 80;
            TextCol.Format = "###,###,##0.00";
            TextCol.Alignment = HorizontalAlignment.Right;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 12);
            TextCol.MappingName = "giaban";
            TextCol.HeaderText = "Giá bán";
            TextCol.Width = 80;
            TextCol.Format = "###,###,##0.00";
            TextCol.Alignment = HorizontalAlignment.Right;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 13);
            TextCol.MappingName = "tt";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            try
            {
                if (row == dataGrid1.CurrentRowIndex)
                {
                    dataGrid1.Select(row);
                    c = Color.White;
                }
                //if (this.dataGrid1[row, 13].ToString().Trim() == "0")
                //    c = Color.Red;
                //else c = Color.Black;
            }
            catch
            {
            }
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
		private void ref_text(long id)
		{
			try
			{
				if (id==0)
				{
					int i=dataGrid1.CurrentCell.RowNumber;
					r=d.getrowbyid(dt,"stt="+long.Parse(dataGrid1[i,0].ToString()));
				}
				else r=d.getrowbyid(dt,"stt="+id);
				if (r!=null)
				{
					stt.Text=r["stt"].ToString();
					manguon.SelectedValue=r["manguon"].ToString();
                    mancc.Text = r["madv"].ToString();
					nhacc.Text=r["tennhacc"].ToString();
					mabd.Text=r["ma"].ToString();
					tenbd.Text=r["tenbd"].ToString();
					tenhc.Text=r["tenhc"].ToString();
					dvt.Text=r["dang"].ToString();
					handung.Text=r["handung"].ToString();
					losx.Text=r["losx"].ToString();
					d_soluong=(r["tondau"].ToString()!="")?decimal.Parse(r["tondau"].ToString()):0;
					d_sotien=(r["sttondau"].ToString()!="")?decimal.Parse(r["sttondau"].ToString()):0;
					d_dongia=(r["dongia"].ToString()!="")?decimal.Parse(r["dongia"].ToString()):0;
					d_giaban=(r["giaban"].ToString()!="")?decimal.Parse(r["giaban"].ToString()):0;
					d_slnhap=(r["slnhap"].ToString()!="")?decimal.Parse(r["slnhap"].ToString()):0;
					d_slxuat=(r["slxuat"].ToString()!="")?decimal.Parse(r["slxuat"].ToString()):0;
					soluong.Text=d_soluong.ToString("###,###,##0.0");
                    dongia.Text = d_dongia.ToString("###,###,##0.00");
                    sotien.Text = d_sotien.ToString("###,###,##0.00");
                    giaban.Text = d_giaban.ToString("###,###,##0.00");
					l_id=long.Parse(stt.Text);
					d_soluongcu=d_soluong;
				}
			}
			catch{}
		}
		private void load_dm()
		{
            dtdmbd = d.get_data("select a.ma,trim(a.ten)||' '||a.hamluong as ten,trim(a.tenhc)||'/'||b.ten as tenhc,a.dang,a.id from " + user + ".d_dmbd a left join " + user + ".d_dmhang b on a.mahang=b.id where a.hide=0 order by a.ten").Tables[0];
			listDMBD.DataSource=dtdmbd;
		}
		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			//this.Close();
		}
		private void Filter_dmbd(string ten)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listDMBD.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="ten like '%"+ten.Trim()+"%'"+" or tenhc like '%"+ten.Trim()+"%'";
			}
			catch{}
		}

		private void tenbd_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listDMBD.Focus();
			else if (e.KeyCode==Keys.Enter)
			{
				if (listDMBD.Visible) listDMBD.Focus();
				else SendKeys.Send("{Tab}");
			}
		}

		private void tenbd_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==tenbd)
			{
				Filter_dmbd(tenbd.Text);
                listDMBD.BrowseToDmbd(tenbd, mabd, handung, mabd.Location.X, mabd.Location.Y + mabd.Height - 2, mabd.Width + lTen.Width + tenbd.Width + lTenhc.Width + tenhc.Width + ldvt.Width + dvt.Width, mabd.Height + 3);
			}
		}

		private void tenbd_Validated(object sender, System.EventArgs e)
		{
			if(!listDMBD.Focused) listDMBD.Hide();
			if (tenbd.Text!="" && mabd.Text=="")
			{
				r=d.getrowbyid(dtdmbd,"ten='"+tenbd.Text+"'");
				if (r!=null)
				{
					mabd.Text=r["ma"].ToString();
					dvt.Text=r["dang"].ToString();
					tenhc.Text=r["tenhc"].ToString();
				}
			}
			SendKeys.Send("{F4}");
		}

		private void ena_object(bool ena)
		{
			dataGrid1.Enabled=!ena;
			manguon.Enabled=ena;
			mabd.Enabled=ena;
			tenbd.Enabled=ena;
			soluong.Enabled=ena;
			handung.Enabled=ena;
			losx.Enabled=ena;
            mancc.Enabled = ena;
            nhacc.Enabled = ena;
			giaban.Enabled=ena;
			dongia.Enabled=ena;
			sotien.Enabled=ena;
			butMoi.Enabled=!ena;
			butSua.Enabled=!ena;
			butLuu.Enabled=ena;
			butBoqua.Enabled=ena;
			butHuy.Enabled=!ena;
			butIn.Enabled=!ena;
			butKetthuc.Enabled=!ena;
        }

		private void emp_detail()
		{
			l_id=0;
            mancc.Text = ""; nhacc.Text = "";
            mabd.Text = ""; tenbd.Text = ""; tenhc.Text = "";
            dvt.Text = ""; soluong.Text = "";
            sotien.Text = ""; dongia.Text = "";
            handung.Text = ""; losx.Text = "";
            giaban.Text = "0"; stt.Text = "";
			d_soluongcu=0;
			d_slnhap=0;
            d_slxuat = 0; d_sotien = 0; d_giaban = 0; d_dongia = 0;
		}
		private void butMoi_Click(object sender, System.EventArgs e)
		{
			if (d.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng"+s_mmyy.Substring(0,2)+" năm "+s_mmyy.Substring(2,2)+"đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống",DAL.AccessData.Msg);
				return;
			}
            if (dtdmbd.Rows.Count == 0) load_dm();
			ena_object(true);
			emp_detail();
			mabd.Focus();
		}
		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (dt.Rows.Count==0) return;
			if (d.bKhoaso(s_mmyy))
			{
                MessageBox.Show("Số liệu tháng" + s_mmyy.Substring(0, 2) + " năm " + s_mmyy.Substring(2, 2) + "đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống", DAL.AccessData.Msg);
				return;
			}
            dtsave = dt.Copy();
			l_id=long.Parse(stt.Text);
			ena_object(true);
            mabd.Enabled = false;
            tenbd.Enabled = false;
            soluong.Focus();
		}

		private bool KiemtraDetail()
		{
			i_mabd=0;
			if (mabd.Text=="" && tenbd.Text=="")
			{
				mabd.Focus();
				return false;
			}
			if ((mabd.Text=="" && tenbd.Text!="") || (mabd.Text!="" && tenbd.Text==""))
			{
				if (mabd.Text=="")
				{
					MessageBox.Show("Nhập mã thuốc !",DAL.AccessData.Msg);
					mabd.Focus();
					return false;
				}
				else if (tenbd.Text=="")
				{
					MessageBox.Show("Nhập tên thuốc!",DAL.AccessData.Msg);
					tenbd.Focus();
					return false;
				}
			}
			r=d.getrowbyid(dtdmbd,"ma='"+mabd.Text+"'");
			if (r==null)
			{
				MessageBox.Show("Mã thuốc không hợp lệ !",DAL.AccessData.Msg);
				mabd.Focus();
				return false;
			}
			i_mabd=int.Parse(r["id"].ToString());
			if (soluong.Text=="" || soluong.Text=="0.00" || soluong.Text=="0")
			{
				MessageBox.Show("Nhập số lượng !",DAL.AccessData.Msg);
				soluong.Focus();
				return false;
			}
			d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
			d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
			d_sotien=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
			d_giaban=(giaban.Text!="")?decimal.Parse(giaban.Text):d_dongia;
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if (!KiemtraDetail()) return;
			bool bEdit=l_id!=0;
            if (l_id == 0)
            {
                l_id = d.get_capid;
            }
            decimal _dongia = Math.Round(d_sotien / d_soluong,10);
            if (bEdit)
            {
                d.upd_theodoisua(d.get_capid_theodoisua, mabd.Text, l_id, i_mabd, tenbd.Text, d_soluongcu, i_userid, "TD", "d_tonkhoct_" + s_mmyy, 0, 1, 0);
            }
			if (!d.upd_tonkhoct(s_mmyy,int.Parse(manguon.SelectedValue.ToString()),i_makho,l_id,i_mabd,handung.Text,losx.Text,int.Parse(mancc.Text),d_soluong,d_giaban,d_dongia,"tondau"))
			{
				MessageBox.Show("Không cập nhật được thông tin tồn kho !",DAL.AccessData.Msg);
				return;
			}
			d.upd_tonkhoth(s_mmyy,i_makho,int.Parse(manguon.SelectedValue.ToString()),i_mabd,d_soluong,"tondau");
			if (bEdit) d.upd_tonkho_th(s_mmyy,i_makho);
			ena_object(false);
			load_grid();
			ref_text(0);
			butMoi.Focus();
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			ena_object(false);
			butMoi.Focus();
		}

		private void manguon_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				SendKeys.Send("{Tab}");
			}
		}

		private void mabd_Validated(object sender, System.EventArgs e)
		{
			if (mabd.Text!="")
			{
				r=d.getrowbyid(dtdmbd,"ma='"+mabd.Text+"'");
				if (r!=null) 
				{
					tenbd.Text=r["ten"].ToString();
					tenhc.Text=r["tenhc"].ToString();
					dvt.Text=r["dang"].ToString();
				}
			}
			if(!listDMBD.Focused) listDMBD.Hide();
		}

		private void listDMBD_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)	mabd_Validated(null,null);
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text(0);
		}

		private void sotien_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_sotien=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
                sotien.Text = d_sotien.ToString("###,###,##0.00");
			}
			catch{}
			tinh_giatri();
		}

		private void soluong_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
                soluong.Text = d_soluong.ToString("###,###,##0.0");
				if (d_soluong+d_slnhap-d_slxuat<0)
				{
					MessageBox.Show("Số lượng không hợp lệ (Tồn kho sẽ bị âm) !",DAL.AccessData.Msg);
					soluong.Text=d_soluongcu.ToString("###,###,##0.0");
					soluong.Focus();
				}
			}
			catch{}
			tinh_giatri();
		}

		private void tinh_giatri()
		{
			try
			{
				d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
				if (dongia.Text.Trim()!="")
				{
					d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
					d_sotien=Math.Round(d_soluong*d_dongia,3);
					sotien.Text=d_sotien.ToString("###,###,##0.00");
				}
				else
				{	
					d_sotien=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
					d_dongia=Math.Round(d_sotien/d_soluong,3);
                    dongia.Text = d_dongia.ToString("###,###,##0.00");
				}
                giaban.Text = d_dongia.ToString("###,###,##0.00");
			}
			catch{}
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (dt.Rows.Count==0) return;
				if (d.bKhoaso(s_mmyy))
				{
					MessageBox.Show("Số liệu tháng"+s_mmyy.Substring(0,2)+" năm "+s_mmyy.Substring(2,2)+" đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống",DAL.AccessData.Msg);
					return;
				}
				if (d_slnhap!=0 || d_slxuat!=0)
				{
					MessageBox.Show(tenbd.Text.Trim()+" "+dvt.Text.Trim()+"\nĐã nhập/xuất không cho phép hủy !",DAL.AccessData.Msg);
					return;
				}
				if (MessageBox.Show("Đồng ý hủy thông tin này ?",DAL.AccessData.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
				{
					r=d.getrowbyid(dtdmbd,"ma='"+mabd.Text+"'");
					if (r!=null)
					{
                        d.execute_data("delete from " + user + ".d_tonkhoct_"+s_mmyy+" where makho=" + i_makho + " and stt=" + l_id);
						d.upd_tonkhoth("delete",s_mmyy,i_makho,int.Parse(r["id"].ToString()),int.Parse(manguon.SelectedValue.ToString()),decimal.Parse(soluong.Text),"tondau");
						load_grid();
						ref_text(0);
					}
				}
			}
			catch{}
		}

		private void giaban_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_giaban=(giaban.Text!="")?decimal.Parse(giaban.Text):0;
                giaban.Text = d_giaban.ToString("###,###,##0.00");
			}
			catch{giaban.Text="0";}
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			string sql="select b.ma,trim(b.ten)||' '||b.hamluong as ten,b.tenhc,b.dang,a.tondau,a.tondau*e.giamua as sttondau, c.stt, c.ten as nhombd, e.manguon, d.ten as tennguon,e.giamua,e.giaban "+
				" from "+user+s_mmyy+".d_tonkhoct a,"+user+".d_dmbd b,"+user+".d_dmnhom c,"+user+".d_dmnguon d,"+user+s_mmyy+".d_theodoi e"+
				" where a.stt=e.id and a.mabd=b.id and b.manhom=c.id and e.manguon=d.id and a.tondau<>0 and a.makho="+i_makho+" order by b.ten";
			DataTable ldt=d.get_data(sql).Tables[0];
            //string tenfile=(bGiaban)?"d_tondau_gban.rpt":"d_tondau.rpt";
            //frmReport f=new frmReport(d,ldt,tenfile,this.Text.Trim().ToUpper(),"","","","","","","","","");
            //f.ShowDialog();			
		}

		private void nhomcc_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab) SendKeys.Send("{Tab}");
		}

		private void mabd_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==mabd)
			{
				Filter_mabd(mabd.Text);
				listDMBD.BrowseToDmbd(mabd,tenbd,handung,mabd.Location.X,mabd.Location.Y + mabd.Height-2,mabd.Width+lTen.Width+tenbd.Width+lTenhc.Width+tenhc.Width-32,mabd.Height+5);
			}		
		}

		private void Filter_mabd(string ma)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listDMBD.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="ma like '%"+ma.Trim()+"%'";
			}
			catch{}
		}

		private void dongia_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
                dongia.Text = d_dongia.ToString("###,###,##0.00");
			}
			catch{dongia.Text="0";}
			tinh_giatri();
		}

		private void manguon_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			
		}

		private void tim_Enter(object sender, System.EventArgs e)
		{
			timkiem.Text="";
		}

		private void tim_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==timkiem) RefreshChildren(timkiem.Text);
		}

        public void RefreshChildren(string text)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "tenbd like '%" + text.Trim() + "%'" + " or tenhc like '%" + text.Trim() + "%' or ma like '%" + text.Trim() + "%'";
                if (text != "") ref_text(0);
                else ref_text(l_id);
            }
            catch { }
        }

        private void nhacc_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == nhacc)
            {
                Filter_dmnx(nhacc.Text);
                listnhacc.BrowseToDmnx(nhacc, mancc, soluong);
            }
        }
        private void Filter_dmnx(string ten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listnhacc.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "ten like '%" + ten.Trim() + "%'";
            }
            catch { }
        }

        private void nhacc_Validated(object sender, EventArgs e)
        {
            if (!listnhacc.Focused) listnhacc.Hide();
        }

        private void nhacc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) listnhacc.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (listnhacc.Visible) listnhacc.Focus();
                else SendKeys.Send("{Tab}");
            }
        }

        private void bNut_SizeChanged(object sender, EventArgs e)
        {
            bNut.Location = new Point((this.Width - 628) / 2 + 1, (this.Height - 470) + 433);
        }

        private void handung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void losx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void timkiem_MouseDown(object sender, MouseEventArgs e)
        {
            if (timkiem.Text.Trim() == "Tìm kiếm")
            {
                timkiem.Text = "";
            }
        }

        private void timkiem_Validated(object sender, EventArgs e)
        {
            if (timkiem.Text.Trim() == "")
            {
                timkiem.Text = "Tìm kiếm";
            }
        }
        private void MaskDigit(KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8 || e.KeyChar == '.') e.Handled = false;
            else e.Handled = true;
        }

        private void handung_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8) e.Handled = false;
            else e.Handled = true;
        }

        private void dongia_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskDigit(e);
        }
	}
}
